package com.glls.java2212.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glls.java2212.common.LayuiPageVo;
import com.glls.java2212.mapper.StudentMapper;
import com.glls.java2212.pojo.Student;
import com.glls.java2212.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public LayuiPageVo<Student> studentPageData(Map<String, Object> params) {
        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Student> students =  studentMapper.selectByParams(params);

        // mybatis 的分页数据结构
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        LayuiPageVo<Student> studentLayuiPageVo = new LayuiPageVo<>();
        studentLayuiPageVo.setCode(0);
        studentLayuiPageVo.setMsg("学生分页列表数据");
        studentLayuiPageVo.setCount(studentPageInfo.getTotal());  // 总记录数
        studentLayuiPageVo.setData(studentPageInfo.getList());  // 分页列表数据

        return studentLayuiPageVo;
    }

    @Override
    public int deleteStudentById(Integer id) {
        return studentMapper.deleteStudentById(id);
    }

    @Override
    public int addStudent(Student data) {
        return studentMapper.addStudent(data);
    }


}

